// releasePage/pages/homePageInfo/brashCard/brashCard.js
const app = getApp();
import Http from '../../../../utils/http.js';
const http = new Http();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        ipxBottom: 18,
        authority: '',
        type: 2, //2 nfc测试点 3测试类型
        showCover: false,
        tabsArr: [
            {
                name: '闸机刷卡',
                value: '00',
                checked: true,
            },
            {
                name: 'POS刷卡',
                value: '01',
                checked: false,
            },
        ],
        current: '00',
        inputValue: '',
        sceneDescription: '',
        companyContent: [],
        selectNum: 0,
    },
    editCompany() {
        if (app.powerIdentity('00,01')) return; //权限判断
        wx.navigateTo({
            url: `./editBrashCard/editBrashCard?current=${this.data.current}`,
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getNfcDeviceList();
        this.setData({
            type: options.current,
            authority: app.globalData.authority,
        });
        if (app.globalData.isIpx) {
            this.setData({
                ipxBottom: 52,
            });
        }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    refresh() {
        let that = this;
        that.setData({});
        that.getNfcDeviceList();
    },
    choiceBtn(e) {
        let _that = this;
        //测试点为单选   测试路线多选
        if (_that.data.type == 2) {
            if (
                !_that.data.companyContent[e.currentTarget.dataset.index]
                    .checked
            ) {
                if (_that.data.type == 2) {
                    for (let i = 0; i < _that.data.companyContent.length; i++) {
                        let index = 'companyContent[' + i + '].checked';
                        _that.setData({
                            [index]: false,
                        });
                    }
                }
                let index =
                    'companyContent[' +
                    e.currentTarget.dataset.index +
                    '].checked';
                _that.setData({
                    [index]: !_that.data.companyContent[
                        e.currentTarget.dataset.index
                    ].checked,
                    selectNum: 1,
                });
            } else {
                let index =
                    'companyContent[' +
                    e.currentTarget.dataset.index +
                    '].checked';
                _that.setData({
                    [index]: !_that.data.companyContent[
                        e.currentTarget.dataset.index
                    ].checked,
                    selectNum: 0,
                });
            }
        } else {
            let selectNums = 0;
            let index =
                'companyContent[' + e.currentTarget.dataset.index + '].checked';
            _that.setData({
                [index]: !_that.data.companyContent[
                    e.currentTarget.dataset.index
                ].checked,
            });
            for (let i = 0; i < _that.data.companyContent.length; i++) {
                if (_that.data.companyContent[i].checked) {
                    selectNums = selectNums + 1;
                }
            }
            _that.setData({
                selectNum: selectNums,
            });
        }
    },
    radioChange(e) {
        let _that = this;
        for (let i = 0; i < _that.data.companyContent.length; i++) {
            let index = 'companyContent[' + i + '].checked';
            _that.setData({
                [index]: false,
            });
        }
        _that.setData({
            current: e.detail.value,
            selectNum: 0,
        });
    },
    //显示添加网络场景
    showCover(e) {
        let _that = this;
        _that.setData({
            showCover: true,
        });
    },
    hideCover(e) {
        this.setData({
            showCover: false,
        });
    },
    stopEvent(e) {
        return;
    },
    getScene(e) {
        this.setData({
            inputValue: e.detail.value,
        });
    },
    sceneDescription(e) {
        this.setData({
            sceneDescription: e.detail.value,
        });
    },
    //保存
    comeBack(e) {
        let _that = this;
        let company = '';
        let companyId = '';
        for (let i = 0; i < _that.data.companyContent.length; i++) {
            if (
                _that.data.companyContent[i].checked &&
                (_that.data.companyContent[i].type == _that.data.current ||
                    _that.data.companyContent[i].type == '10')
            ) {
                company =
                    company +
                    ',' +
                    _that.data.companyContent[i].devicemanufacturer;
                companyId = companyId + ',' + _that.data.companyContent[i].id;
            }
        }
        console.log(companyId.substr(1));
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        prevPage.setData({
            nfcCompany: company.substr(1),
            nfcCompanyId: companyId.substr(1),
        });
        wx.navigateBack({
            delta: 1, // 返回上一级页面。
        });
    },
    //添加刷卡设备
    addScene(e) {
        let _that = this;
        if (_that.data.inputValue) {
            //添加
            _that.setData({
                showCover: false,
            });
            wx.request({
                url:
                    app.globalData.rootPath +
                    `cjMap/createNfcDeviceManufacturer?authId=${app.globalData.authId}`,
                method: 'POST',
                header: {
                    'content-type': 'application/json',
                },
                data: {
                    delflg: '00', //删除标志 00未删除 01已删除 02待审核10草稿
                    devicemanufacturer: _that.data.inputValue,
                    devicemanufacturerdesc: _that.data.sceneDescription,
                    type: _that.data.current,
                },
                success: function (res) {
                    console.log(res);
                    if (res.data.status == 0) {
                        wx.showToast({
                            title: 'OK',
                            icon: 'success',
                            duration: 2000,
                        });
                        _that.setData({
                            companyContent: [],
                            inputValue: '',
                            sceneDescription: '',
                        });
                        _that.getNfcDeviceList();
                    } else {
                        wx.showToast({
                            title: `${res.data.errmsg}`,
                            icon: 'none',
                            duration: 2000,
                        });
                        _that.setData({
                            inputValue: '',
                            sceneDescription: '',
                        });
                    }
                },
            });
        } else {
            _that.setData({
                showCover: false,
            });
        }
    },
    // 获取网络场景列表
    getNfcDeviceList() {
        let _that = this;
        http.getRequest(
            `cjMap/getNfcDeviceManufacturerList?authId=${app.globalData.authId}&createtimeorder=asc`
        ).then((res) => {
            if (res.status == 0) {
                console.log(res);
                for (let i = 0; i < res.data.length; i++) {
                    res.data[i].checked = false;
                }
                _that.setData({
                    companyContent: res.data,
                    selectNum: 0,
                });
            }
        });
    },
});
